package cn.akwangl.controller;

import com.sankuai.inf.leaf.common.Result;
import com.sankuai.inf.leaf.service.SegmentService;
import com.sankuai.inf.leaf.service.SnowflakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 美团分布式Id Demo.
 * <p>
 * 1. 号段模式<br/>
 * 2. 雪花算法<br/>
 * <p>
 * 项目地址: <a href="https://github.com/Meituan-Dianping/Leaf">Leaf</a>
 *
 * @author youyongkun
 * @date 2022/5/19 10:46
 * @since 0.0.3
 */
@RestController
@RequestMapping("/leaf")
public class LeafIdController {

    private SegmentService segmentService;

    private SnowflakeService snowflakeService;

    @Autowired
    public void setSegmentService(SegmentService segmentService) {
        this.segmentService = segmentService;
    }

    @Autowired
    public void setSnowflakeService(SnowflakeService snowflakeService) {
        this.snowflakeService = snowflakeService;
    }

    /**
     * 号段模式
     *
     * @return Id
     */
    @GetMapping("/segment")
    public Result segment() {
        return segmentService.getId("leaf-segment-test");
    }

    /**
     * 雪花算法
     *
     * @return Id
     */
    @GetMapping("/snowflake")
    public Result snowflake() {
        return snowflakeService.getId("");
    }
}
